﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class HUISliderEventScript : HUIComponent {

	[HideInInspector]
	public enUIEventID m_onValueChangedEventID;

	private Slider m_slider;

	private int m_DescribeCount;

	private Text[] m_Describes;

	private Text m_Handletext;

	private float m_value;

	public float value
	{
		get
		{
			if (this.m_slider)
			{
				return this.m_slider.value;
			}
			return -1f;
		}
		set
		{
			this.m_value = value;
			if (this.m_slider && this.m_value <= this.m_slider.maxValue && this.m_value >= 0f)
			{
				this.m_slider.value = this.m_value;
				this.m_Handletext.text = this.m_Describes[(int)this.m_value].text;
			}
		}
	}

	public int MaxValue
	{
		get
		{
			if (this.m_slider)
			{
				return (int)this.m_slider.maxValue;
			}
			return 0;
		}
	}

	public bool Enabled
	{
		get
		{
			return this.m_slider && this.m_slider.interactable;
		}
		set
		{
			if (this.m_slider)
			{
				this.m_slider.interactable = value;
			}
		}
	}

	public override void Initialize(HUIFormScript formScript)
	{
		if (this.m_isInitialized)
		{
			return;
		}
		this.m_slider = base.gameObject.GetComponent<Slider>();
		if (this.m_slider == null)
		{
			return;
		}
		this.m_DescribeCount = base.transform.Find("Background").childCount;
		this.m_Describes = new Text[this.m_DescribeCount];
		for (int i = 0; i < this.m_DescribeCount; i++)
		{
			this.m_Describes[i] = base.transform.Find(string.Format("Background/Text{0}", i + 1)).GetComponent<Text>();
		}
		this.m_Handletext = base.transform.Find("Handle Slide Area/Handle/Text").GetComponent<Text>();
		this.m_slider.onValueChanged.RemoveAllListeners();
		this.m_slider.onValueChanged.AddListener(new UnityAction<float>(this.OnSliderValueChanged));
		base.Initialize(formScript);
	}

	protected override void OnDestroy()
	{
		this.m_slider = null;
		this.m_Describes = null;
		this.m_Handletext = null;
		base.OnDestroy();
	}

	private void OnSliderValueChanged(float value)
	{
		if (value < 0f || value >= (float)this.m_DescribeCount)
		{
			return;
		}
		this.value = value;
		this.DispatchSliderEvent();
	}

	private void DispatchSliderEvent()
	{
		if (this.m_onValueChangedEventID == enUIEventID.None)
		{
			return;
		}
		HUIEvent uIEvent = HUIEventManager.Instance.GetUIEvent();
		uIEvent.m_srcFormScript = this.m_belongedFormScript;
		uIEvent.m_srcWidget = base.gameObject;
		uIEvent.m_srcWidgetScript = this;
		uIEvent.m_pointerEventData = null;
		uIEvent.m_eventID = this.m_onValueChangedEventID;
		uIEvent.m_eventParams.sliderValue = this.value;
		base.DispatchUIEvent(uIEvent);
	}

	public Slider GetSlider()
	{
		return this.m_slider;
	}
}
